/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package da;

import entities.Account;
import entities.Deposit;
import entities.Transfer;
import entities.Withdrawal;
import java.math.BigDecimal;
import java.util.Date;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author QUY_THI
 */
public class AccountAcction {

    private EntityManagerFactory emf = null;

    public AccountAcction() {
        emf = Persistence.createEntityManagerFactory("eBanking-ejbPU");
    }

    public boolean accountDeposit(Account account, BigDecimal amount) {
        boolean check = false;
        EntityManager em = emf.createEntityManager();
        try {
            BigDecimal money = account.getBalance();
            BigDecimal sum = money.add(amount);
            em.getTransaction().begin();
            account.setBalance(sum);
            Deposit de = new Deposit();
            de.setDepositID(0);
            de.setAmount(amount);
            de.setAccount(account);
            de.setDepositDate(new Date());
            em.merge(account);
            em.persist(de);
            em.getTransaction().commit();
            check = true;
        } catch (Exception e) {
            em.getTransaction().rollback();
            check = false;
        } finally {
            if (em != null) {
                em.close();
            }
        }
        return check;
    }

    public boolean accountWithdrawal(Account account, BigDecimal amount) {
        boolean check = false;
        EntityManager em = emf.createEntityManager();
        try {
            BigDecimal money = account.getBalance();
            BigDecimal sub = money.subtract(amount);
            em.getTransaction().begin();
            account.setBalance(sub);
            Withdrawal with = new Withdrawal();
            with.setAccount(account);
            with.setAmount(amount);
            with.setWithdrawalDate(new Date());
            with.setWithdrawalID(0);
            em.merge(account);
            em.persist(with);
            em.getTransaction().commit();
            check = true;
        } catch (Exception e) {
            em.getTransaction().rollback();
            check = false;
        } finally {
            if (em != null) {
                em.close();
            }
        }
        return check;
    }

    public boolean accountTransfer(Account sender, BigDecimal amount, Account receiver) {
        boolean check = false;
        EntityManager em = emf.createEntityManager();
        BigDecimal sendmoney = sender.getBalance();
        BigDecimal receivemoney = receiver.getBalance();
        BigDecimal sub = sendmoney.subtract(amount);
        BigDecimal sum = receivemoney.add(amount);
        em.getTransaction().begin();

        sender.setBalance(sub);
        receiver.setBalance(sum);
        em.merge(sender);
        em.merge(receiver);
        Transfer tr = new Transfer();
        tr.setTransferID(0);
        tr.setAmount(amount);
        tr.setAccount1(sender);
        tr.setAccount(receiver);
        tr.setTransactionDate(new Date());
        em.persist(tr);
        em.getTransaction().commit();
        check = true;
        try {
        } catch (Exception e) {
            em.getTransaction().rollback();
            check = false;
        } finally {
            if (em != null) {
                em.close();
            }
        }
        return check;
    }
}
